package library.multiple_pictures_selector.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import butterknife.ButterKnife;
import library.common.util.LogUtils;

/**
 * @author Created by qiang.hou on 2018/3/2.
 * @version 1.0
 */
public abstract class BaseAppCompatActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.i(getSimpleTag(), "sky_dreaming on onCreate activity = "
                + getSimpleTag());
        if (returnLayoutId() > 0) {
            this.setContentView(returnLayoutId());
            ButterKnife.bind(this);
        }
        initView();
        setListener();

        /*
         * the activity need refresh data on onResume
         */
        if (!refreshOnResume()) {
            initProcess();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.i(getSimpleTag(), "sky_dreaming on onStart activity = "
                + getSimpleTag());
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.i(getSimpleTag(), "sky_dreaming on onResume activity = "
                + getSimpleTag());
        if (refreshOnResume()) {
            initProcess();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.i(getSimpleTag(), "sky_dreaming on onPause activity = "
                + getSimpleTag());
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.i(getSimpleTag(), "sky_dreaming on onStop activity = "
                + getSimpleTag());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.i(getSimpleTag(), "sky_dreaming on onDestroy activity = "
                + getSimpleTag());
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        LogUtils.i(this.getLocalClassName() + "finalized....");
    }

    /**
     * the activity need refresh data on onResume
     *
     * @return boolean
     */
    protected boolean refreshOnResume() {
        return false;
    }

    /**
     * get class name
     *
     * @return class name
     */
    public String getSimpleTag() {
        return getPackageName() + this.getClass().getSimpleName();
    }

    /**
     * Init data and process
     */
    protected void initProcess() {
    }

    /**
     * set listener to view
     */
    protected void setListener() {
    }

    /**
     * Init views
     */
    protected abstract void initView();

    /**
     * get layout id
     *
     * @return layout resource id
     */
    protected abstract int returnLayoutId();

    /**
     * start next activity
     *
     * @param intent            intent
     * @param needForResult     if call startActivityForResult
     * @param requestCode       requestCode for startActivityForResult
     * @param enterAnim         enter animation
     * @param exitAnim          exit animation
     * @param needFinishCurrent if need finish current page
     */
    @SuppressWarnings("unused")
    public void startNextActivity(Intent intent, boolean needForResult,
                                  int requestCode, int enterAnim, int exitAnim, boolean needFinishCurrent) {
        if (needForResult) {
            startActivityForResult(intent, requestCode);
        } else {
            startActivity(intent);
        }
        if (enterAnim > 0 && exitAnim > 0) {
            overridePendingTransition(enterAnim, exitAnim);
        }
        if (needFinishCurrent) {
            finish();
        }
    }
}
